package com.supersit.greensproject.view;


import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.supersit.greensproject.R;
import com.supersit.greensproject.adapter.SpinerAdapter;

import java.util.List;

/**
 * Created by cong on 2016/7/21.
 */
public class MySpinner extends FrameLayout {
    private View view;
    private SpinerAdapter mAdapter;
    private LinearLayout btDropdown;
    private TextView mTView;
    // 设置PopWindow
    private SpinerPopWindow mSpinerPopWindow;
    private Context context;
    private List<String> areaList;

    public MySpinner(Context context, View view, List<String> areaList) {
        super(context);
        this.view = view;
        this.context = context;
        this.areaList = areaList;
        initView();
        initVariable();
    }

    private void initView() {
        btDropdown = (LinearLayout) view.findViewById(R.id.bt_dropdown);
        mTView = (TextView) view.findViewById(R.id.tv_value);
    }

    private void initVariable() {
        mAdapter = new SpinerAdapter(context, areaList);
        mAdapter.refreshData(areaList, 1);
        mSpinerPopWindow = new SpinerPopWindow(context);
        mSpinerPopWindow.setAdatper(mAdapter);
        mSpinerPopWindow.setItemListener(iOnItemSelectListener);
        btDropdown.setOnClickListener(onClickListener);
    }

    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.bt_dropdown:
                    showSpinWindow();
                    break;
            }
        }
    };

    private void showSpinWindow() {
        Log.e("", "showSpinWindow");
        mSpinerPopWindow.setWidth(mTView.getWidth());
        mSpinerPopWindow.showAsDropDown(mTView);
    }

    private SpinerAdapter.IOnItemSelectListener iOnItemSelectListener = new SpinerAdapter.IOnItemSelectListener() {
        @Override
        public void onItemClick(int pos) {
            if (pos >= 0 && pos <= areaList.size()) {
                String value = areaList.get(pos);
                mTView.setText(value.toString());
            }
        }
    };
}
